package com.agricultural.iframe;

import com.agricultural.bean.User;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Locale;

public class UserIframe {
    private JPanel main;
    private JLabel title;
    private JPanel userInfo;
    private JLabel userPermission;
    private JLabel userName;
    private JPanel userOptions;
    private JLabel changeInfo;
    private JLabel productList;
    private JLabel myShop;
    private JLabel goBack;
    private JPanel adminOptions;
    private JLabel userManagement;
    private JLabel productManagement;
    private JLabel sales;
    private JFrame frame;
    private final User user;

    public UserIframe() {
        this.user = Login.user;
        initializeComponents();
        checkAdmin();
        changeInfo.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ChangeUserInfo changeUserInfo = new ChangeUserInfo(null);
            }
        });
        goBack.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                frame.dispose();
                Login login = new Login();
            }
        });
        productList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ShopList shoplist = new ShopList();
            }
        });
        myShop.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                PurchaseList purchaseList = new PurchaseList();
            }
        });
        sales.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                SalesIframe salesIframe = new SalesIframe();
            }
        });
        userManagement.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                UserManagementIframe userManagementIframe = new UserManagementIframe();
            }
        });
        productManagement.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ProductManagementIframe productManagementIframe = new ProductManagementIframe();
            }
        });
    }

    private void initializeComponents() {
        frame = new JFrame("UserIframe");
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private void checkAdmin() {
        boolean isAdmin = user.getIs_admin().equals("1");
        userName.setText("欢迎您：" + user.getUsername());
        userPermission.setText("您的身份为：" + (isAdmin ? "管理员" : "普通用户"));
        if (isAdmin) {
            userOptions.setVisible(false);
            adminOptions.setVisible(true);
        } else {
            userOptions.setVisible(true);
            adminOptions.setVisible(false);
        }
        frame.revalidate();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        main = new JPanel();
        main.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        title = new JLabel();
        Font titleFont = this.$$$getFont$$$(null, Font.BOLD, 22, title.getFont());
        if (titleFont != null) title.setFont(titleFont);
        title.setText("农副产品交易管理系统");
        CellConstraints cc = new CellConstraints();
        main.add(title, cc.xy(1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
        userInfo = new JPanel();
        userInfo.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        main.add(userInfo, cc.xy(1, 3));
        userPermission = new JLabel();
        userPermission.setText("您的身份为：用户");
        userInfo.add(userPermission, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        userName = new JLabel();
        userName.setText("欢迎您：user1");
        userInfo.add(userName, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        userOptions = new JPanel();
        userOptions.setLayout(new FormLayout("fill:d:grow", "center:d:grow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        Font userOptionsFont = this.$$$getFont$$$(null, -1, 18, userOptions.getFont());
        if (userOptionsFont != null) userOptions.setFont(userOptionsFont);
        main.add(userOptions, cc.xy(1, 7, CellConstraints.FILL, CellConstraints.CENTER));
        productList = new JLabel();
        Font productListFont = this.$$$getFont$$$(null, -1, 20, productList.getFont());
        if (productListFont != null) productList.setFont(productListFont);
        productList.setText("商品列表");
        userOptions.add(productList, cc.xy(1, 3, CellConstraints.CENTER, CellConstraints.DEFAULT));
        myShop = new JLabel();
        Font myShopFont = this.$$$getFont$$$(null, -1, 20, myShop.getFont());
        if (myShopFont != null) myShop.setFont(myShopFont);
        myShop.setText("我的购买");
        userOptions.add(myShop, cc.xy(1, 5, CellConstraints.CENTER, CellConstraints.DEFAULT));
        adminOptions = new JPanel();
        adminOptions.setLayout(new FormLayout("fill:d:grow", "center:d:grow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        Font adminOptionsFont = this.$$$getFont$$$(null, -1, 18, adminOptions.getFont());
        if (adminOptionsFont != null) adminOptions.setFont(adminOptionsFont);
        main.add(adminOptions, cc.xy(1, 9, CellConstraints.FILL, CellConstraints.CENTER));
        userManagement = new JLabel();
        Font userManagementFont = this.$$$getFont$$$(null, -1, 20, userManagement.getFont());
        if (userManagementFont != null) userManagement.setFont(userManagementFont);
        userManagement.setText("用户管理");
        adminOptions.add(userManagement, cc.xy(1, 3, CellConstraints.CENTER, CellConstraints.DEFAULT));
        productManagement = new JLabel();
        Font productManagementFont = this.$$$getFont$$$(null, -1, 20, productManagement.getFont());
        if (productManagementFont != null) productManagement.setFont(productManagementFont);
        productManagement.setText("商品管理");
        adminOptions.add(productManagement, cc.xy(1, 5, CellConstraints.CENTER, CellConstraints.DEFAULT));
        sales = new JLabel();
        Font salesFont = this.$$$getFont$$$(null, -1, 20, sales.getFont());
        if (salesFont != null) sales.setFont(salesFont);
        sales.setText("销量统计");
        adminOptions.add(sales, cc.xy(1, 7, CellConstraints.CENTER, CellConstraints.DEFAULT));
        goBack = new JLabel();
        Font goBackFont = this.$$$getFont$$$(null, -1, 20, goBack.getFont());
        if (goBackFont != null) goBack.setFont(goBackFont);
        goBack.setText("返回登录");
        main.add(goBack, cc.xy(1, 11, CellConstraints.CENTER, CellConstraints.DEFAULT));
        changeInfo = new JLabel();
        Font changeInfoFont = this.$$$getFont$$$(null, -1, 20, changeInfo.getFont());
        if (changeInfoFont != null) changeInfo.setFont(changeInfoFont);
        changeInfo.setText("修改信息");
        main.add(changeInfo, cc.xy(1, 5, CellConstraints.CENTER, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return main;
    }

}
